#!/usr/bin/ruby
#
#  octopus4r - a ruby wrapper for the Octopus USB board
#  Copyright (C) 2008 by Michael Conrad <do5mc@friggleware.de>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
#

require 'octopus_native'

require 'octopus4r/constants.rb'
require 'octopus4r/io.rb'
require 'octopus4r/adc.rb'
require 'octopus4r/i2c.rb'
require 'octopus4r/spi.rb'
require 'octopus4r/uart.rb'


module Octopus4r

  Octopus4rVersion = [0, 0, 2].join( '.')


  class Device


    def initialize

      @device = Octopus_native::Octopus_context.new
      value = Octopus_native::octopus_init( @device)
      print "value: #{value}\n"

      @ports = Array.new( 5, nil)
      @pins = Array.new( 38, nil)
      
      @adcs = Array.new( 8, nil)
      @uarts = Array.new( 2, nil)

      @i2c = nil
      @spi = nil

      return
    end


    def open
      Octopus_native::octopus_open( @device)
    end


    def open_id( vendor, product)
      Octopus_native::octopus_open_id( @device, vendor.to_i, product.to_i)
    end


    def open_serial( serial)
      Octopus_native::octopus_open_serial( @device, serial.to_s)
    end

    
    def close
      Octopus_native::octopus_close( @device)
    end


    def reset
      Octopus_native::octopus_reset( @device)
    end


    def hwdesc
      description = "placeholder........"
      Octopus_native::octopus_get_hwdesc( @device, description)
    end

    
    def port( port)
      index = port.to_i - 1

      if index < 0 && index >= 5
        raise "wrong port index"
      end

      @ports[index] = Port.new( @device, index) if @ports[index].nil?

      return @ports[index] 
    end
    
    
    def pin( pin)
      index = pin.to_i - 1
      
      if index < 0 && index >= 44
        raise "wrong pin index"
      end
      
      @pins[index] = Pin.new( @device, index) if @pins[index].nil?
      
      return @pins[index]
    end
    
    
    def adc( adc)
      index = adc.to_i - 1
      
      if index < 0 && index >= 8 
        raise "wrong adc index"
      end
      
      @adcs[index] = ADC.new( @device, index) if @adcs[index].nil?

      return @adcs[index]
    end

    
    def adc_reference=( reference)
      Octopus_native::octopus_adc_ref( @device, reference.to_i)
    end


    def uart( uart)
      index = uart.to_i - 1

      if index < 0 && index >= 2
        raise "wronge uart index"
      end
      
      @uarts[index] = UART.new( @device, index) if @uarts[index].nil?
      
      return @uarts[index]
    end

    
    def i2c
      @i2c = I2C.new( @device) if @i2c.nil?
      
      return @i2c
    end


    def spi
      @spi = SPI.new( @device) if @spi.nil?

      return @spi
    end


  end

end


device = Octopus4r::Device.new

device.open

